package com.ruoyi.serviceItem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.serviceItem.domain.IncomeExpenseRecord;
import com.ruoyi.serviceItem.domain.WithdrawalRecord;
import com.ruoyi.serviceItem.mapper.IncomeExpenseRecordMapper;
import com.ruoyi.serviceItem.service.WithdrawalRecordService;
import com.ruoyi.serviceItem.mapper.WithdrawalRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【withdrawal_record(提现记录表)】的数据库操作Service实现
* @createDate 2025-03-21 14:55:42
*/
@Service
public class WithdrawalRecordServiceImpl extends ServiceImpl<WithdrawalRecordMapper, WithdrawalRecord>
    implements WithdrawalRecordService{
    @Autowired
    private WithdrawalRecordMapper withdrawalMapper;

    @Autowired
    private IncomeExpenseRecordMapper incomeExpenseMapper;

    @Override
    public List<WithdrawalRecord> getAllWithdrawalRecord() {
        return baseMapper.getAllWithdrawalRecord();
    }

    @Override
    public AjaxResult applyWithdrawal(WithdrawalRecord withdrawalRecord) {
        // 1. 创建提现记录
        WithdrawalRecord withdrawal = new WithdrawalRecord();
        withdrawal.setAmount(withdrawalRecord.getAmount());
        withdrawal.setBankCardType(withdrawalRecord.getBankCardType());
        withdrawal.setBankCardNo(withdrawalRecord.getBankCardNo());
        withdrawal.setWithdrawalTime(new Date());
        withdrawal.setAuditStatus(1); // 审核状态：待审核
        withdrawal.setStoreId(1L); // TODO 替换为实际门店ID
        withdrawal.setExt1(withdrawalRecord.getExt1()); // 真实姓名存ext1

        withdrawalMapper.insertWithdrawal(withdrawal);

        // 2. 创建收支记录
        IncomeExpenseRecord incomeExpense = new IncomeExpenseRecord();
        incomeExpense.setWithdrawalId(Math.toIntExact(withdrawal.getWithdrawalId()));
        incomeExpense.setDecreaseAmount(withdrawal.getAmount());
        incomeExpense.setStoreId(1L);

        incomeExpenseMapper.insertWithdrawalRecord(incomeExpense);

        return AjaxResult.success(withdrawal.getWithdrawalId());
    }
}




